

load(file=paste(rawDatDir2,'obstetrics_all.RData', sep = ''))
hospitalCrosswalk = datCut[year==2012, mean(year), by = c("hospital", "hosp_id")]
setnames(hospitalCrosswalk, "hosp_id","hosp_id_alt")
hospitalCrosswalk$V1 = NULL
rm(datCut)

## Developed in fl_compute_deltas.R
load(paste0(datDir,"deltaDat_minPpl_all_5.RData"))

data = grpShrDatXi[is.finite(xiHat),][.id == "main",]
setnames(data,"nGrp","nZip")

zipPSA = c(33024,33068,33309,33311,33312,33313,33314,33317,33319,33321,33322,33323,33324,33325,33328,33330,33351)
ZIP_CLOSER = c(33325,33330,33328,33314,33024)
ZIP_CONSTANT = c(33323,33324,33317,33312)
ZIP_FARTHER = c(33321,33068,33351,33319,33309,33322,33313,33311)

POP_ADJ_CLOSER = (1990/1972)
POP_ADJ_CONSTANT = (1952/1971)
POP_ADJ_FARTHER = (6167/6293)

TIME = c(9.655,20.842,20.218,18.216,15.870,16.787,5.975,9.861,21.179,21.915,13.709,15.840,9.425,11.284,4.494,9.351,16.758)

dataPSA <- data[pat_zip %in% zipPSA]
dataPSA$hosp_id_alt = as.character(dataPSA$hosp_id_alt)
dataPSA = merge(dataPSA, hospitalCrosswalk, by = c("hosp_id_alt"))


hospitalsInMarket = c("PLANTATION GENERAL HOSPITAL","BOCA RATON REGIONAL HOSPITAL", "BROWARD HEALTH MEDICAL CENTER",
                      "BROWARD HEALTH CORAL SPRINGS", "HOLY CROSS HOSPITAL INC", "MEMORIAL HOSPITAL MIRAMAR",  "MEMORIAL HOSPITAL WEST",
                      "MEMORIAL REGIONAL HOSPITAL","NORTHWEST MEDICAL CENTER","WEST BOCA MEDICAL CENTER" )
dataPSA[, includeHospital := (hospital %in% hospitalsInMarket)]
dataPSA[, hospShareOut2 := sum((includeHospital == FALSE) * hospShare), by = c("pat_zip")]
dataPSA[, hospShareOut := hospShareOut + hospShareOut2]
dataPSA$hospShareOut2 = NULL
dataPSAMkt = copy(dataPSA[includeHospital == TRUE,])
dataPSAMkt$includeHospital = NULL
dataPSAMkt$.id = NULL
dataPSAMkt$year = NULL
dataPSAMkt$hospShareNew = 0

## Adjust Delta for new outside option
dataPSAMkt[,deltaHat := log(hospShare) - log(hospShareOut)]
## New nZip to account for projections of market growth
dataPSAMkt[pat_zip %in% ZIP_CLOSER,nZipNew := nZip * POP_ADJ_CLOSER]
dataPSAMkt[pat_zip %in% ZIP_CONSTANT,nZipNew := nZip * POP_ADJ_CONSTANT]
dataPSAMkt[pat_zip %in% ZIP_FARTHER,nZipNew := nZip * POP_ADJ_FARTHER]
## New time for new hospital
dataPSAMkt[, time_current_new := time_current]

newData = as.data.table(dataPSAMkt[hosp_id_alt == "100167",])
for(ZIP_NUM in 1:length(zipPSA)) {
  newData[hosp_id_alt == "100167", hosp_id_alt := "999999"]
  newData[hosp_id_alt == "999999" & pat_zip == zipPSA[ZIP_NUM], time_current_new := TIME[ZIP_NUM]]
}
dataPSAMkt = rbind(dataPSAMkt,newData, use.names = TRUE)


